Association vs. Aggregation vs. Composition
Association, Aggregation, and Composition are types of relationships that describe the interaction and dependency between objects in Object-Oriented Analysis and Design (OOAD). Each type has its own unique characteristics:
Association
Association represents a general connection or relationship between two classes, where each class is aware of the other. This relationship doesn't imply ownership; both classes are relatively independent of each other. It is typically depicted as a simple line connecting two classes.
Characteristics
- No ownership implied (neither class owns the other).
- The objects involved in the association can exist independently.
- Usually denoted with a simple line between classes in UML diagrams.
Example
A student enrolls in courses, and a course has students enrolled in it, but they don't own each other. The Student and Course classes are associated since they interact, but they can exist independently.
Student ------------ Course
class Student {
private String name;
public void enroll(Course course) {
System.out.println(name + " enrolled in " + course.getName());
}
}
class Course {
private String name;
public String getName() {
return name;
}
}
Aggregation
Aggregation is a specialized form of association that represents a "whole-part" relationship between classes. Here, one class is a container or aggregator, but it doesn’t imply strong ownership. The aggregated objects can exist independently outside the lifecycle of the aggregator.
Characteristics
- Represents a whole-part relationship.
- The part (or aggregated) can exist independently of the whole.
- It’s a weaker form of composition, signified by a hollow diamond at the end of the line connecting the aggregator to the part.
Example
A library is composed of books (whole-part), but a book can still exist outside of a library. If a library is closed, the books can still exist in other libraries or as standalone objects.
Library ◇----------- Book
class Library {
private List<Book> books;
public void addBook(Book book) {
books.add(book);
}
}
class Book {
private String title;
public String getTitle() {
return title;
}
}
Composition
Composition is a more specific type of aggregation that implies a stronger ownership between classes. In this relationship, the "whole" class cannot exist without its "part" class, and if the whole object is destroyed, its parts are also destroyed.
Characteristics
- Represents a whole-part relationship with strong ownership.
- The lifecycle of the part class depends on the whole.
- Illustrated with a filled diamond on the UML diagram, connecting the whole to the part.
Example
A house is composed of rooms, but rooms cannot exist independently of the house. If the house is demolished, the rooms cease to exist as well.
House ◆----------- Room
class House {
private List<Room> rooms;
public House() {
rooms = new ArrayList<>();
}
public void addRoom(Room room) {
rooms.add(room);
}
}
class Room {
private String name;
public Room(String name) {
this.name = name;
}
}
Summary of Differences
| Relationship Type | Represents | Lifecycle Dependency | Symbol |
|---|---|---|---|
| Association | Simple relationship | Independent | Simple line |
| Aggregation | Whole-part | Part can exist independently of the whole | Hollow diamond |
| Composition | Strong whole-part | Part’s lifecycle depends on the whole | Filled diamond |